package com.huaxindata.im.main.home;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;

import com.huaxindata.im.R;
import com.huaxindata.im.main.chathistory.MessageFragment;
import com.huaxindata.im.main.contactview.ContactFragment;
import com.huaxindata.im.main.manager.ContactManager;
import com.huaxindata.im.main.manager.HistoryContactManager;
import com.huaxindata.im.main.manager.UserManager;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.service.MessageService;
import com.huaxindata.im.service.OnMessageReceiveListener;

public class MainActivity extends AppCompatActivity implements
        RadioGroup.OnCheckedChangeListener{

    private MessageService mMessageService;
    private boolean mIsBind;

    private FragmentManager mFragmentManager;
    private Fragment mTestFragment,mCurrentFragment;
    private MessageFragment mMessageFragment;
    private ContactFragment mContactFragment;

    private RadioGroup mRadioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ContactManager.initData();
        HistoryContactManager.init(this);
        bindToService();
        init();
    }

    private void init() {
        mFragmentManager=getSupportFragmentManager();
        mTestFragment=new TestFragment();
        mMessageFragment =new MessageFragment();
        mContactFragment=new ContactFragment();

        mRadioGroup = (RadioGroup) findViewById(R.id.main_radioGroup);
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    /**
     * 绑定到后台接收消息的service
     */
    private void bindToService() {
        Intent intent = new Intent(this, MessageService.class);
        bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);
        mIsBind=true;
    }

    private ServiceConnection mServiceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mMessageService = ((MessageService.MyBinder)service).getService();
            //放在这里可以避免空指针异常
            mMessageService.connectAndSubscribe(UserManager.getClientId(),ContactManager.getTopics());
            mMessageService.addMessageReceiveListener(mListener);
            mIsBind=true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mMessageService.removeMessageReceiveListener(mListener);
            mMessageService.stopSelf();
            mMessageService=null;
        }
    };
    /**
     * 与后台接收消息的service解决绑定
     */
    private void unBindService(){
        if (mIsBind) {
            unbindService(mServiceConnection);
            mIsBind=false;
        }
    }
    /**
     * 底部的RadioGroup选中的RadioButton的切换
     */
    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        if (checkedId == R.id.main_radioBtn_finding) {
            switchFragment(mTestFragment);
        } else if (checkedId == R.id.main_radioBtn_message) {
            switchFragment(mMessageFragment);
        } else if (checkedId == R.id.main_radioBtn_workshop) {
            switchFragment(mTestFragment);
        } else if (checkedId == R.id.main_radioBtn_contact){
            switchFragment(mContactFragment);
        }else if (checkedId==R.id.main_radioBtn_profile){
            switchFragment(mTestFragment);
        }
    }
    //在底部的导航栏中用于切换fragment
    private void switchFragment(Fragment fragment){
        if (fragment == null) {
            return;
        }
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (mCurrentFragment == null) {
            transaction.add(R.id.main_fragment_container, fragment);
        } else {
            if (fragment.isAdded()) {
                transaction.hide(mCurrentFragment).show(fragment);
            } else {
                transaction.hide(mCurrentFragment).add(R.id.main_fragment_container, fragment);
            }
        }
        transaction.commit();
        mCurrentFragment=fragment;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unBindService();
    }
    private OnMessageReceiveListener mListener =new OnMessageReceiveListener() {
        @Override
        public void receiveMessage(ChatMessage hxMessage) {
            mMessageFragment.initData();
        }
    };

}
